package controller

import (
	"mini-app/internal/app/web/service"
	"mini-app/internal/models"
	"strconv"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type OnlineEvent struct{}

// List 在线活动列表
func (OnlineEvent) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	status, _ := strconv.Atoi(c.Query("status"))
	oepnID := GetOpenID(c)
	list, total := service.OnlineEvent{}.List(c.Request.Context(), oepnID, c.Query("name"), status, page, limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, nil)
}

// Detail 活动详情
func (OnlineEvent) Detail(c *gin.Context) {
	detail, err := service.OnlineEvent{}.Detail(
		c.Request.Context(),
		c.GetString("openid"),
		c.Query("online_event_id"),
	)
	response.JSON(c, detail, err)
}

// Join 参加活动
func (OnlineEvent) Join(c *gin.Context) {
	var params struct {
		OnlineEventID string             `json:"online_event_id"`
		UserAddress   models.UserAddress `json:"user_address"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	err := service.OnlineEvent{}.Join(
		c.Request.Context(),
		c.GetString("openid"),
		params.OnlineEventID,
		params.UserAddress,
	)
	response.JSON(c, nil, err)
}

// SupportDetail 助力详情
func (OnlineEvent) SupportDetail(c *gin.Context) {
	detail, err := service.OnlineEvent{}.SupportDetail(c.Request.Context(), c.Query("support_id"))
	response.JSON(c, detail, err)
}

// Support 助力
func (OnlineEvent) Support(c *gin.Context) {
	var params struct {
		SupportID string `json:"support_id"`
	}
	c.ShouldBindBodyWithJSON(&params)
	err := service.OnlineEvent{}.Support(c.Request.Context(), c.GetString("openid"), params.SupportID)
	response.JSON(c, nil, err)
}

func (OnlineEvent) WinnerList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	list, total := service.OnlineEvent{}.WinnerList(c, c.Query("online_event_id"), page, limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, nil)
}
